package com.zhazha.earlybird.dao;

import com.zhazha.earlybird.beans.RemindMeetingInfo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CollectMeettingDAO {
	
	private SQLiteDatabase mDatabase;
	
	private static final String COLLECT_MEETING_TABLE = "collect_meeting";
	
	private static final String COLLECT_MEETING_ID = "id"; 
	private static final String COLLECT_MEETING_CAM_REC_TITLE = "campus_recruit_title";
	private static final String COLLECT_MEETING_TITLE = "title";
	private static final String COLLECT_MEETING_PLACE = "place";
	private static final String COLLECT_MEETING_TIME = "time";
	private static final String COLLECT_MEETING_REMIND_TIME = "remind_time";
	private static final String COLLECT_MEETING_LONGITUDE = "longitude";
	private static final String COLLECT_MEETING_LATITUDE = "latitude";
	private static final String COLLECT_MEETING_OPEN_REMIND = "open_remind";
	
	public CollectMeettingDAO(SQLiteDatabase datebase) {
		this.mDatabase = datebase;
	}
	
	public RemindMeetingInfo queryById(int id) {
		RemindMeetingInfo info = null;
		
		try {
			
			 String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(COLLECT_MEETING_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
	
					info = new RemindMeetingInfo();
					info.setId(c.getInt(c.getColumnIndex(COLLECT_MEETING_ID)));
					info.setCampusRecruitTitle(c.getString(c.getColumnIndex(COLLECT_MEETING_CAM_REC_TITLE)));
					info.setLatitude(c.getDouble(c.getColumnIndex(COLLECT_MEETING_LATITUDE)));
					info.setLongitude(c.getDouble(c.getColumnIndex(COLLECT_MEETING_LONGITUDE)));
					info.setOPenRemind(c.getInt(c.getColumnIndex(COLLECT_MEETING_OPEN_REMIND)));
					info.setPlace(c.getString(c.getColumnIndex(COLLECT_MEETING_PLACE)));
					info.setRemindTime(c.getString(c.getColumnIndex(COLLECT_MEETING_REMIND_TIME)));
					info.setTime(c.getString(c.getColumnIndex(COLLECT_MEETING_TIME)));
					info.setTitle(c.getString(c.getColumnIndex(COLLECT_MEETING_TITLE)));
				
				return info;
				
			}
			
			return info;
		}
		catch (Exception e) {
			e.printStackTrace();
			return info;
		}
	}
	
	public RemindMeetingInfo[] queryAll() {
		RemindMeetingInfo[] info = null;
		try {
			
			 Cursor c = this.mDatabase.query(COLLECT_MEETING_TABLE, null, null, null, null, null, "id asc");
			
			if(c.moveToFirst()) {
				info = new RemindMeetingInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					info[i] = new RemindMeetingInfo();
					info[i].setId(c.getInt(c.getColumnIndex(COLLECT_MEETING_ID)));
					info[i].setCampusRecruitTitle(c.getString(c.getColumnIndex(COLLECT_MEETING_CAM_REC_TITLE)));
					info[i].setLatitude(c.getDouble(c.getColumnIndex(COLLECT_MEETING_LATITUDE)));
					info[i].setLongitude(c.getDouble(c.getColumnIndex(COLLECT_MEETING_LONGITUDE)));
					info[i].setOPenRemind(c.getInt(c.getColumnIndex(COLLECT_MEETING_OPEN_REMIND)));
					info[i].setPlace(c.getString(c.getColumnIndex(COLLECT_MEETING_PLACE)));
					info[i].setRemindTime(c.getString(c.getColumnIndex(COLLECT_MEETING_REMIND_TIME)));
					info[i].setTime(c.getString(c.getColumnIndex(COLLECT_MEETING_TIME)));
					info[i].setTitle(c.getString(c.getColumnIndex(COLLECT_MEETING_TITLE)));
					c.moveToNext();
				}
				return info;
				
			}
			
			return info;
		}
		catch (Exception e) {
			e.printStackTrace();
			return info;
		}
	}
	
	public void update(RemindMeetingInfo info) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(info.getId())};
			
			ContentValues cv = new ContentValues();
			cv.put(COLLECT_MEETING_ID, info.getId());
			cv.put(COLLECT_MEETING_CAM_REC_TITLE, info.getCampusRecruitTitle());
			cv.put(COLLECT_MEETING_LATITUDE, info.getLatitude());
			cv.put(COLLECT_MEETING_LONGITUDE, info.getLongitude());
			cv.put(COLLECT_MEETING_OPEN_REMIND, info.getOpenRemind());
			cv.put(COLLECT_MEETING_PLACE, info.getPlace());
			cv.put(COLLECT_MEETING_REMIND_TIME, info.getRemindTime());
			cv.put(COLLECT_MEETING_TIME, info.getTime());
			cv.put(COLLECT_MEETING_TITLE, info.getTitle());
			
			this.mDatabase.update(COLLECT_MEETING_TABLE, cv, whereClause, whereArgs);
			
		}catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	public void insert(RemindMeetingInfo info) {
		
		try {
			ContentValues cv = new ContentValues();
			
			cv.put(COLLECT_MEETING_ID, info.getId());
			cv.put(COLLECT_MEETING_CAM_REC_TITLE, info.getCampusRecruitTitle());
			cv.put(COLLECT_MEETING_LATITUDE, info.getLatitude());
			cv.put(COLLECT_MEETING_LONGITUDE, info.getLongitude());
			cv.put(COLLECT_MEETING_OPEN_REMIND, info.getOpenRemind());
			cv.put(COLLECT_MEETING_PLACE, info.getPlace());
			cv.put(COLLECT_MEETING_REMIND_TIME, info.getRemindTime());
			cv.put(COLLECT_MEETING_TIME, info.getTime());
			cv.put(COLLECT_MEETING_TITLE, info.getTitle());
			
			this.mDatabase.insert(COLLECT_MEETING_TABLE, null, cv);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void deleteById(int id) {
		
		try{
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(COLLECT_MEETING_TABLE, whereClause, whereArgs);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

}
